<template>
  <div class="main">
    <el-dialog
      title="添加"
      :visible.sync="visible"
      :close-on-click-modal="false"
      @close="colseDialog"
    >
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="right"
        label-width="80px"
        style="width: 500px; margin-left: 50px"
      >
        <el-form-item label="投顾名称" prop="investName">
          <el-input v-model="temp.investName" placeholder="请输入投顾名称" />
        </el-form-item>
        <el-form-item label="联系人" prop="investContacts">
          <el-input v-model="temp.investContacts" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="手机号码" prop="investMobile">
          <el-input
            v-model.trim="temp.investMobile"
            placeholder="请输入手机号码"
          />
        </el-form-item>
        <el-form-item label="成立日期" prop="investCommittime">
          <el-date-picker
            v-model="temp.investCommittime"
            type="date"
            placeholder="请选择成立日期"
            style="width: 420px"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="微信号" prop="investWechat">
          <el-input v-model="temp.investWechat" placeholder="请输入微信号" />
        </el-form-item>
        <el-form-item label="邮箱" prop="investEmail">
          <el-input v-model="temp.investEmail" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="合作意向" prop="investType">
          <el-select
            v-model="temp.investType"
            placeholder="请选择合作意向"
            style="width: 420px"
          >
            <el-option
              :label="item.name"
              :value="item.id"
              v-for="item in typeList"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="渠道来源" prop="investChannel">
          <el-autocomplete
            v-model="temp.investChannel"
            :fetch-suggestions="querySearch"
            @select="handleSelect"
            style="width: 420px"
            placeholder="请输入渠道来源"
          >
            <template slot-scope="{ item }">
              <div class="name">{{ item }}</div>
            </template>
          </el-autocomplete>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="colseDialog()">取消</el-button>
        <el-button type="primary" @click="createData()"> 提交 </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { addAdviser } from "@/api/investManage";

import { mapGetters } from "vuex";
export default {
  props: {
    dialogFormVisible: {
      type: Boolean,
    },
  },
  data: () => {
    return {
      visible: false,
      temp: {
        investChannel: "",
        investName: "",
        investContacts: "",
        investMobile: "",
        investCommittime: "",
        investWechat: "",
        investEmail: "",
        investType: "",
      },
      typeList: [
        { id: 1, name: "MOM" },
        { id: 2, name: "FOF" },
        { id: 3, name: "专户" },
        { id: 4, name: "其他" },
      ],
      rules: {
        investName: [
          { required: true, message: "投顾名称必填", trigger: "change" },
        ],
        // investContacts: [{ required: true, message: '联系人必填', trigger: 'change' }],
        // investMobile: [{ validator: checkPhone, required: true, trigger: 'blur' }],
        // investEmail: [{ validator: checkEmail, trigger: 'blur' }],
        // investCommittime: [{ required: true, message: '成立日期必填', trigger: 'change' }]
      },
    };
  },
  computed: {
    ...mapGetters(["userId"]),
  },
  watch: {
    dialogFormVisible: {
      handler(val) {
        this.visible = val;
        if (val) {
          this.$nextTick(() => {
            this.$refs["dataForm"].resetFields();
          });
        }
      },
      deep: true,
    },
  },

  methods: {
    querySearch(queryString, cb) {
      const restaurants = this.investChannelList;
      const results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants;
      cb(results);
    },
    handleSelect(item) {
      this.temp.investChannel = item;
    },
    createData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          addAdviser({
            ...this.temp,
            investCereatedby: this.userId,
          }).then((res) => {
            if (res.code == 0) {
              this.$notify({
                message: "修改成功",
                type: "success",
                duration: 2000,
              });
              this.$emit("update:dialogFormVisible", false);
              this.$router.push("InvestManage");
            }
          });
        }
      });
    },
    colseDialog() {
      this.$emit("update:dialogFormVisible", false);
    },
  },
};
</script>
